package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    RoomLabelService roomLabelService;
    @Autowired
    RoomFacilityService roomFacilityService;
    @Autowired
    RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    RoomLeaseTermService roomLeaseTermService;
    @Autowired
    RoomAttrValueService roomAttrValueService;
    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    RoomFacilityMapper roomFacilityMapper;
    @Autowired
    RoomLabelMapper roomLabelMapper;
    @Autowired
    RoomPaymentTypeMapper roomPaymentTypeMapper;
    @Autowired
    RoomLeaseTermMapper roomLeaseTermMapper;
    @Autowired
    RoomAttrValueMapper roomAttrValueMapper;
    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        Long RoomId = roomSubmitVo.getId();
        if(RoomId!= null){
            //更新
            //删除图片
            LambdaQueryWrapper<GraphInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(GraphInfo::getItemId,RoomId);
            queryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoService.remove(queryWrapper);
            //删除房间标签
            LambdaQueryWrapper<RoomLabel> RoomLabelQueryWrapper = new LambdaQueryWrapper<>();
            RoomLabelQueryWrapper.eq(RoomLabel::getRoomId,RoomId);
            roomLabelService.remove(RoomLabelQueryWrapper);
            //删除房间属性
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueQueryWrapper.eq(RoomAttrValue::getRoomId,RoomId);
            roomAttrValueService.remove(roomAttrValueQueryWrapper);
            //删除房间配套
            LambdaQueryWrapper<RoomFacility> roomFacilityQueryWrapper = new LambdaQueryWrapper<>();
            roomFacilityQueryWrapper.eq(RoomFacility::getRoomId,RoomId);
            roomFacilityService.remove(roomFacilityQueryWrapper);
            //删除房间支付方式
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeQueryWrapper.eq(RoomPaymentType::getRoomId,RoomId);
            roomPaymentTypeService.remove(roomPaymentTypeQueryWrapper);
            //删除房间租期
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermQueryWrapper.eq(RoomLeaseTerm::getRoomId,RoomId);
            roomLeaseTermService.remove(roomLeaseTermQueryWrapper);
        }
        this.saveOrUpdate(roomSubmitVo);
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if(!CollectionUtils.isEmpty(labelInfoIds)){
            ArrayList<RoomLabel> roomLabelInfoList = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
             RoomLabel roomLabel = RoomLabel.builder()
                     .roomId(roomSubmitVo.getId())
                     .labelId(labelInfoId)
                     .build();
                     roomLabelInfoList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelInfoList);
        }
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if(!CollectionUtils.isEmpty(attrValueIds)){
            ArrayList<RoomAttrValue> roomAttrValueInfoList = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder()
                        .roomId(roomSubmitVo.getId())
                        .attrValueId(attrValueId)
                        .build();
                roomAttrValueInfoList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValueInfoList);
        }
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            ArrayList<RoomFacility> roomFacilityInfoList = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder()
                        .roomId(roomSubmitVo.getId())
                        .facilityId(facilityInfoId)
                        .build();
                        roomFacilityInfoList.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilityInfoList);
        }
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if(!CollectionUtils.isEmpty(paymentTypeIds)){
            ArrayList<RoomPaymentType> roomPaymentTypeInfoList = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder()
                        .roomId(roomSubmitVo.getId())
                        .paymentTypeId(paymentTypeId)
                        .build();
                        roomPaymentTypeInfoList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeInfoList);
        }
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if(!CollectionUtils.isEmpty(leaseTermIds)){
            ArrayList<RoomLeaseTerm> roomLeaseTermInfoList = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder()
                        .roomId(roomSubmitVo.getId())
                        .leaseTermId(leaseTermId)
                        .build();
                        roomLeaseTermInfoList.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTermInfoList);
        }
    }

    @Override
    public IPage<RoomItemVo> pageRoomItemByQuery(IPage<RoomItemVo> itemVoIPage,@Param("queryVo") RoomQueryVo queryVo) {
       IPage<RoomItemVo> roomItemVoIPage = baseMapper.selectRoomItemByQuery(itemVoIPage,queryVo);
        return roomItemVoIPage;
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //房间信息
        RoomInfo roomInfo = baseMapper.selectById(id);
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        //公寓信息
        Long apartmentId = roomInfo.getApartmentId();
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(apartmentId);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        //图片信息
        LambdaQueryWrapper<GraphInfo> graphInfoQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoQueryWrapper.eq(GraphInfo::getItemId, roomInfo.getId());
        graphInfoQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        List<GraphInfo> graphInfos = graphInfoService.list(graphInfoQueryWrapper);
        List<GraphVo> graphVoList =graphInfos.stream().map(graphInfo -> {
            GraphVo graphVo = new GraphVo();
            BeanUtils.copyProperties(graphInfo, graphVo);
            return graphVo;
        }).collect(Collectors.toList());
        roomDetailVo.setGraphVoList(graphVoList);
        //配套信息
        List<FacilityInfo> facilityInfoList= roomFacilityMapper.selectListByRoomId(id);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        //标签信息
        List<LabelInfo> labelInfoList = roomLabelMapper.selectListByRoomId(id);
        roomDetailVo.setLabelInfoList(labelInfoList);
        //支付方式
        List<PaymentType> paymentTypeList = roomPaymentTypeMapper.selectListByRoomId(id);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        //可选租期
        List<LeaseTerm> leaseTermList = roomLeaseTermMapper.selectListByRoomId(id);
        roomDetailVo.setLeaseTermList(leaseTermList);
        //属性信息
        List<AttrValueVo> attrValueVoList = roomAttrValueMapper.selectListByRoomId(id);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        return roomDetailVo;
    }

    @Override
    public void removeRoomById(Long id) {

       LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemId,id);
        graphQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
        graphInfoService.remove(graphQueryWrapper);

        LambdaQueryWrapper<RoomFacility> roomFacilityQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityQueryWrapper.eq(RoomFacility::getRoomId,id);
        roomFacilityService.remove(roomFacilityQueryWrapper);

        LambdaQueryWrapper<RoomLabel> roomLabelQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelQueryWrapper.eq(RoomLabel::getRoomId,id);
        roomLabelService.remove(roomLabelQueryWrapper);

        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeQueryWrapper.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeService.remove(roomPaymentTypeQueryWrapper);

        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermQueryWrapper.eq(RoomLeaseTerm::getRoomId,id);
        roomLeaseTermService.remove(roomLeaseTermQueryWrapper);

        LambdaQueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueQueryWrapper.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueService.remove(roomAttrValueQueryWrapper);

        super.removeById(id);
    }

    @Override
    public List<Long> selectLeaseListByApartmentId(Long apartmentId) {
        List<Long> leaseRoomIds = baseMapper.selectLeaseListByApartmentId(apartmentId);
        return leaseRoomIds;
    }
}




